var app = getApp();

Page({
  data: {
    sId: null,
    changeLesson: '选择课程',
    showModalStatus: false,
    animationData: null,
    courseName: null,
    today: null,
    yesterday: null,
    yeYesterday: null,
    pageBackgroundColor: '#E6F3F9',
    courseId: null,
    items: [{
      name: '电子版',
      value: '电子版'
    },
    {
      name: '纸质版',
      value: '纸质版',
    }
    ],
    type_list: null
  },
  onShow(e) {
    var that = this;
    this.setData({
      sId: app.globalData.sId
    })
    console.log("這是這個學生的ID" + that, this.data.sId);
    getCourses(that, that.data.sId);
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var that = this;
    var arrayObj = new Array(); //创建一个数组     
    that.setData({
      type_list: e.detail.value
    })
  },

  //提交作業到服務器存儲
  submitHomework: function (e) {


    var that = this;
    wx.request({
      url: `https://geek-team.xin/homework/set`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: this.data.sId,
        title: e.detail.value.hw_title,
        submitTime: e.detail.value.hw_time,
        type: this.data.type_list,
        content: e.detail.value.hw_content,
        course: this.data.courseId,
        courseTime: this.data.courseTime

      },
      success(res) {
        app.globalData.socket.emit("setHomework", `{"course":"${that.data.courseId}",
        "courseTime":"${that.data.courseTime}"}`, (e) => console.log(e))
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1000
        })
        console.log(res)
      },
      fail: (e) => console.log(e)
    })
  },
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    var _name = e.currentTarget.dataset.name;
    var _id = e.currentTarget.dataset.id;
    var _time = e.currentTarget.dataset.time;

    this.util(currentStatu)
    this.setData({
      pageBackgroundColor: '#E6F3F9',
      changeLesson: _name,
      courseId: _id,
      courseTime: _time,

    })
  },

  util: function (currentStatu) {
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    });

    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  }
})






function getCourses(that, sid) {
  console.log(that)
  wx.request({
    url: `https://geek-team.xin/schedule/wx_getTeacherScheduleByThreeDay`,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      tId: sid
    },
    success(res) {
      var _yeYesterday = JSON.parse(res.data.yeYesterday)
      var _today = JSON.parse(res.data.today)
      var _yesterday = JSON.parse(res.data.yesterday)

      if (res.data != []) {
        that.setData({
          today: _today,
          yesterday: _yesterday,
          yeYesterday: _yeYesterday,
        })
        console.log(res.data)
      }
    },
    fail: (e) => console.log(e)
  })
}